package com.elong.android.specialhouse.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 获取设施列表icon的工具
 * @author boyan
 *
 */
	public class GetFacilitiesIcon {
	private static GetFacilitiesIcon mInstance;
	private static String[] list_image = null; 
	private static Context mContext;
	private static List<Map<String, Object>> cateList = new ArrayList<Map<String, Object>>();
	private GetFacilitiesIcon(){
		
	}
	public static GetFacilitiesIcon getInstance(Context context){
		
		if(mInstance == null){
			mContext = context;
			mInstance = new GetFacilitiesIcon();
			getImages();
		}
		return mInstance;
	}
	private static void getImages(){
		try {  
		//得到assets/facilitiesicom/目录下的所有文件的文件名，以便后面打开操作时使用  
			list_image = mContext.getAssets().list("facilitiesicon");  
			} catch (IOException e1) {  
		    // TODO Auto-generated catch block  
				e1.printStackTrace();  
		}  
		for(int i=0;i<list_image.length;++i)  
		{  
		    InputStream open = null;  
		    try {  
				  String temp = "facilitiesicon/"+list_image[i];  
				  open = mContext.getAssets().open(temp);  
				  Bitmap bitmap = BitmapFactory.decodeStream(open);  
				  Map<String, Object> map = new HashMap<String, Object>();  
				  String[] temps = list_image[i].split("_");
				  String id = temps[temps.length - 1].replace(".png","");
				  map.put("id", id);  
				  map.put("iv", bitmap);  
				  cateList.add(map);  
				  // Assign the bitmap to an ImageView in this layout  
		    } catch (IOException e) {  
				  e.printStackTrace();  
		    } finally {  
		    		if (open != null) {  
		    		try {  
				      open.close();  
			    } catch (IOException e) {  
				      e.printStackTrace();  
			    }  
		    	}  
		    		}  
		}
	
	}
	public Bitmap getImage(int id){
		Bitmap bitmap = null;
		String Idstr = Integer.valueOf(id).toString();


		for(int i = 0;i < cateList.size();i++){
			Map<String, Object> map = cateList.get(i);
			if(Idstr.equals(map.get("id"))){
				bitmap = (Bitmap) map.get("iv");
				break;
			}
		}
		return bitmap;
	}
}
